class Solution {
    StringBuffer ret = new StringBuffer();
    public void preOrder(TreeNode root) {
        ret.append(root.val);
        if(root.left!=null) {
            ret.append("(");
            preOrder(root.left);
            ret.append(")");
        } else {
            if(root.right==null) {
                return;
            } else {
                ret.append("()");
            }
        }
        if(root.right==null) {
            return;
        } else {
            ret.append("(");
            preOrder(root.right);
            ret.append(")");
        }
    }

    public String tree2str(TreeNode root) {
        if(root==null) return null;
        preOrder(root);
        return ret.toString();
    }
}
public class lc606 {
}
